package com.sunmouse.toollib.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


import com.sunmouse.toollib.utils.system.AnimUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Kvin on 2016/12/27.
 */
public class JumpUtils {
    public static final boolean DEFAULT_STATUS = false;


    /**
     * simple jump
     */
    public static void simpleJump(Activity from, Class<?> to, Intent intent, boolean isFinish, AnimUtils.AnimStyle animStyle) {
        if (from == null) return;
        if (to != null) {
            Intent i = new Intent(from, to);
            from.startActivity(i);
        } else if (intent != null) {
            from.startActivity(intent);
        }
        AnimUtils.setAnim(from, animStyle);
        //res finish
        if (isFinish) {
            from.finish();
        }
    }

    /**
     * simple jump
     */
    public static void simpleJump(Activity from, Class<?> to) {
        if (to == null) return;
        simpleJump(from, to, null, DEFAULT_STATUS, AnimUtils.getDefaultJumpAnim());
    }

    /**
     * simple jump
     */
    public static void simpleJump(Activity from, Intent intent) {
        if (intent == null) return;
        simpleJump(from, null, intent, DEFAULT_STATUS, AnimUtils.getDefaultJumpAnim());
    }

    /**
     * simple jump
     */
    public static void simpleJump(Activity from, Class<?> to, AnimUtils.AnimStyle animStyle) {
        if (to == null) return;
        simpleJump(from, to, null, DEFAULT_STATUS, animStyle);
    }

    /**
     * simple jump
     */
    public static void simpleJump(Activity from, Intent intent, AnimUtils.AnimStyle animStyle) {
        if (intent == null) return;
        simpleJump(from, null, intent, DEFAULT_STATUS, animStyle);
    }

    /**
     * simple jump  with judging whether from needs to be  finished
     */
    public static void simpleJump(Activity from, Class<?> to, boolean isFinish) {
        if (to == null) return;
        simpleJump(from, to, null, isFinish, AnimUtils.getDefaultJumpAnim());
    }

    /**
     * simple jump
     */
    public static void simpleJump(Activity from, Intent intent, boolean isFinish) {
        if (intent == null) return;
        simpleJump(from, null, intent, isFinish, AnimUtils.getDefaultJumpAnim());
    }

    public static void simpleJump(Activity from, Class<?> to, Intent intent, boolean isFinish) {
        simpleJump(from, to, intent, isFinish, AnimUtils.AnimStyle.LEFT_IN_FADE_OUT);
    }


    /**
     * jump with params
     */
    public static void jumpWithParams(Activity from, Class<?> to, HashMap<String, String> params, Bundle bundle, boolean isFinish, AnimUtils.AnimStyle animStyle) {
        if (from == null || to == null) return;
        Intent intent = new Intent(from, to);
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                intent.putExtra(entry.getKey(), entry.getValue());
            }
        } else if (bundle != null) {
            intent.putExtras(bundle);
        }

        from.startActivity(intent);
        AnimUtils.setAnim(from, animStyle);
        if (isFinish) {
            from.finish();
        }
    }

    /**
     * jump with bundle
     */
    public static void jumpWithParams(Activity from, Class<?> to, Bundle data) {
        jumpWithParams(from, to, null, data, DEFAULT_STATUS, AnimUtils.getDefaultJumpAnim());
    }
    /**
     * jump with bundle
     */
    public static void jumpWithParams(Activity from, Class<?> to, Bundle data,boolean isFinish) {
        jumpWithParams(from, to, null, data, isFinish, AnimUtils.getDefaultJumpAnim());
    }
    /**
     * jump with params
     */
    public static void jumpWithParams(Activity from, Class<?> to, HashMap<String, String> params) {
        jumpWithParams(from, to, params, null, DEFAULT_STATUS, AnimUtils.getDefaultJumpAnim());
    }


    /**
     * start activity for result
     */
    public static void jumpWithResult(Activity from, Class<?> to, Intent intent, int requestCode, AnimUtils.AnimStyle animStyle) {
        Intent mIntent = null;
        if (from == null) return;

        if (to != null) {
            mIntent = new Intent(from, to);
        } else if (intent != null) {
            mIntent = intent;
        }
        if (mIntent == null) return;
        from.startActivityForResult(mIntent, requestCode);
        AnimUtils.setAnim(from, animStyle);
    }

    public static void jumpWithResult(Activity from, Class<?> to, int requestCode, AnimUtils.AnimStyle animStyle) {
        jumpWithResult(from, to, null, requestCode, animStyle);
    }

    public static void jumpWithResult(Activity from, Class<?> to, int requestCode) {
        jumpWithResult(from, to, null, requestCode, AnimUtils.getDefaultJumpAnim());
    }


}
